// import {  } from '@/api/user'
import { getToken, setToken, getTokenKey, setTokenKey, removeToken, setConstatnsTable } from '@/utils/auth'
import { resetRouter } from '@/router'
import { login, logout, getInfo } from '@/svc/account'
// import store from '@/store'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  key: getTokenKey(),
  updateTokenExtra: '', // 令牌更新字段名称
  messageCountExtra: '', // 信息数量更新字段名称
  messageCount: 0, // 当前信息数量
  introduction: '',
  roles: [],
  errDeny: '',
  errGotoLogin: '',
  superRole: ''
}

const mutations = {
  SET_TOKEN: (state, token) => {
    setTokenKey(state.key)
    setToken(token)
    state.token = token
  },
  SET_MESSAGE: (state, message) => {
    state.messageCount = message
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_KEY: (state, key) => {
    state.key = key
  },
  SET_UPDATE_TOKEN_EXTRA: (state, name) => {
    state.updateTokenExtra = name
  },
  SET_MESSAGE_COUNT_EXTRA: (state, name) => {
    state.messageCountExtra = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_DENY_ERR: (state, err) => {
    state.errDeny = err
  },
  SET_GOTO_LOGIN_ERR: (state, err) => {
    state.errGotoLogin = err
  },
  SET_SUPER: (state, superRole) => {
    state.superRole = superRole
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ user: username.trim(), password: password }).then((response) => {
        // const { data } = response
        // const res = data['account.Login'][0]
        const res = response
        // console.log('res', res['error'])
        if (res['error']) {
          // 登录失败
          reject(res['error'])
        } else {
          // 登录成功
          const { key, token, constants } = res.data
          // console.log('login..res.data', res.data)
          commit('SET_KEY', key)
          commit('SET_TOKEN', token)

          // console.log(errors)
          commit('SET_GOTO_LOGIN_ERR', constants['gotoLogin'])
          commit('SET_DENY_ERR', constants['deny'])
          commit('SET_UPDATE_TOKEN_EXTRA', constants['updateToken'])
          commit('SET_MESSAGE_COUNT_EXTRA', constants['messageCount'])
          // console.log('errors', constants)

          // setTokenKey(key)
          // setToken(token)
          setConstatnsTable(constants)
          // console.log('store.getters.token set', store.getters.token)
          resolve()
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    // console.log('getInfo...')
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        // console.log('getInfo', response)
        const { data } = response

        if (!data) {
          reject('Verification failed, please Login again.')
        }

        const { name, avatar, introduction, superRole } = data
        let { roles } = data
        // roles must be a non-empty array
        // if (!roles || roles.length <= 0) {
        //   reject('getInfo: roles must be a non-null array!')
        // }
        if (!roles || roles.length <= 0) {
          roles = ['']
          // reject('getInfo: roles must be a non-null array!')
        }

        commit('SET_ROLES', roles)
        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        commit('SET_INTRODUCTION', introduction)
        commit('SET_SUPER', superRole)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken()
        resetRouter()

        // reset visited views and cached views
        // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
        dispatch('tagsView/delAllViews', null, { root: true })

        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  }

  // dynamically modify permissions
  // changeRoles({ commit, dispatch }, role) {
  //   return new Promise(async resolve => {
  //     const token = role + '-token'

  //     commit('SET_TOKEN', token)
  //     setToken(token)

  //     const { roles } = await dispatch('getInfo')

  //     resetRouter()

  //     // generate accessible routes map based on roles
  //     const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })

  //     // dynamically add accessible routes
  //     router.addRoutes(accessRoutes)

  //     // reset visited views and cached views
  //     dispatch('tagsView/delAllViews', null, { root: true })

  //     resolve()
  //   })
  // }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
